package com.softer.wxzj.common.factory;

import com.softer.wxzj.common.util.AddressUtils;
import com.softer.wxzj.common.util.IpUtils;
import com.softer.wxzj.common.util.SpringUtils;
import com.softer.wxzj.entity.SysLoginInfo;
import com.softer.wxzj.service.impl.SysLoginInfoServiceImpl;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;

/**
 * 异步工厂（产生任务用）
 *
 * @author liuhulu
 */
public class AsyncFactory implements Runnable {
    private String username;
    private String status;
    private String message;
    private HttpServletRequest request;

    public AsyncFactory(HttpServletRequest request,String username, String status, String message) {
        this.message = message;
        this.username = username;
        this.status = status;
        this.request = request;
    }

    @Override
    public void run() {
        final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        String ip = IpUtils.getIpAddr(request);
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        // 封装对象
        SysLoginInfo loginInfo = new SysLoginInfo();
        loginInfo.setLoginName(username);
        loginInfo.setIp(ip);
        loginInfo.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
        loginInfo.setBrowser(browser);
        loginInfo.setOs(os);
        loginInfo.setMsg(message);
        // 日志状态
        loginInfo.setStatus(status);
        // 插入数据
        SpringUtils.getBean(SysLoginInfoServiceImpl.class).insertLoginInfo(loginInfo);
    }
}
